package client.ui;

import java.util.ArrayList;
import java.util.List;

import client.SearchService;
import client.SearchServiceAsync;
import client.dtos.RepositoryFileDTO;
import client.dtos.RevisionDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Label;

public class RepositoryFileHistoryDialog extends DialogBox {
	
	private final RepositoryFileDTO rfDTO;
	private SearchServiceAsync searchService = (SearchServiceAsync)GWT.create(SearchService.class); 
	private final Label versionNumberLabel = new Label("r");
	private final Label message = new Label();
	private final Label committerLabel = new Label();
	private final Label dateLabel = new Label();
	private final List<RevisionDTO> dtoList = new ArrayList<RevisionDTO>();
	private int currentRevision;
	private int page = 0;
	
	public RepositoryFileHistoryDialog(final RepositoryFileDTO rfDTO) {
		setWidth("400px");
		setText(rfDTO.getPath());
		final DockPanel dockPanel = new DockPanel();
		this.rfDTO = rfDTO;
		refresh();
		
		
		final DockPanel buttonDockPanel = new DockPanel();
		buttonDockPanel.setSpacing(1);
		buttonDockPanel.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
		final Button rightButton = new Button("->");
		final Button leftButton = new Button("<-");
		
		final int remainingSpace = 400-leftButton.getOffsetWidth()-rightButton.getOffsetWidth();
		
		final String width = remainingSpace/3+"px";
		versionNumberLabel.setWidth(width);
		versionNumberLabel.setHorizontalAlignment(Label.ALIGN_CENTER);
		
		committerLabel.setWidth(width);
		committerLabel.setHorizontalAlignment(Label.ALIGN_CENTER);
		
		dateLabel.setWidth(width);
		dateLabel.setHorizontalAlignment(Label.ALIGN_CENTER);
		
		
		leftButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				currentRevision++;
				if (currentRevision==dtoList.size()) {
					page++;
					refresh();
				}
				displayCurrentRevision();
			}
			
		});
		rightButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				if (currentRevision>0) {
					currentRevision--;
					if (currentRevision==0) {
						if (page>0) {
							page--;
							refresh();
						}
					}
					displayCurrentRevision();
				}
				
			}
			
		});
		buttonDockPanel.add(leftButton, DockPanel.WEST);
		buttonDockPanel.add(rightButton, DockPanel.EAST);
		buttonDockPanel.add(versionNumberLabel, DockPanel.WEST);
		buttonDockPanel.add(dateLabel, DockPanel.CENTER);
		buttonDockPanel.add(committerLabel, DockPanel.EAST);
		dockPanel.add(buttonDockPanel, DockPanel.NORTH);
		dockPanel.add(message, DockPanel.CENTER);
		
		final DockPanel bottomDockPanel = new DockPanel();
		final Button closeButton = new Button("Close");
		closeButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				dtoList.clear();
				hide();
			}
			
		});
		bottomDockPanel.add(closeButton, DockPanel.WEST);
		dockPanel.add(bottomDockPanel, DockPanel.SOUTH);
		setWidget(dockPanel);
		center();
	}

	private void refresh() {
		searchService.getMostRecentRevisionForFile(this.rfDTO.getRepositoryFileID(), page, new AsyncCallback<List<RevisionDTO>>() {

			public void onFailure(Throwable caught) {
				Window.alert("Failure while getting next history item:" + caught.getMessage());
			}

			public void onSuccess(List<RevisionDTO> result) {
				dtoList.clear();
				dtoList.addAll(result);
				currentRevision = 0;
				displayCurrentRevision();
			}
			
		});
	}
	
	private void displayCurrentRevision() {
		if (dtoList!=null && dtoList.size()>0) {
			if (currentRevision==dtoList.size()) {
				currentRevision=0;
			}
			if (currentRevision>=dtoList.size()) {
				Window.alert("Current revision:"+currentRevision+", size:"+dtoList.size());
			} else {
				final RevisionDTO revisionDTO = dtoList.get(currentRevision);
				versionNumberLabel.setText("r"+revisionDTO.getRevisionNumber());
				committerLabel.setText(revisionDTO.getRevisionUser());
				message.setText(revisionDTO.getRevisionMessage());
				dateLabel.setText(revisionDTO.getRevisionDateAsString());
			}
		}
	}
	
}
